﻿package core.scene {
	
	import base.BaseMc;
	import flash.display.MovieClip;
	import core.events.BgSoundEvent;
	import core.events.CmdEvent;
	import core.net.socket.cmd.BluetoothConnectCmd;
	import core.User;
	import core.net.socket.cmd.BluetoothOnlineCmd;
	import core.net.socket.cmd.BluetoothOfflineCmd;
	import flash.display.Bitmap;
	import flash.display.PixelSnapping;
	import flash.display.BitmapData;
	import flash.display.LoaderInfo;
	import core.display.FlowerAnimation2;
	import core.events.FlowerAnimationEvent;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import core.display.LoginMenu;
	import core.display.LeftBackButton;
	import core.display.RightBackButton;
	import utils.UtilScreen;
	import flash.events.MouseEvent;
	import core.net.socket.cmd.BluetoothDisConnectCmd;
	import core.display.ClassBg;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.events.Event;
	import core.dialog.DialogMessage;
	import core.events.MessageDialogEvent;
	import core.display.AlphaBg;
	import flash.display.SimpleButton;
	import core.net.socket.cmd.WindowMinCmd;
	import core.dialog.DialogBluetoothDisconnect;
	import core.net.socket.cmd.GetFlowerRankCmd;
	import com.adobe.serialization.json.JSON;
	import flash.display.Graphics;
	public class SceneClass extends BaseMc {
		private var bg:ClassBg;
		private var bgSound:Sound;
		private var bgSoundChannel:SoundChannel;
		private var preScene:MovieClip;
		private var flowerData :Array=new Array();
		private var loader:Loader;
		private var flowerAnim:FlowerAnimation2;
		public var _timer:Timer = new Timer(3000,0);
		public var _studentTimer:Timer = new Timer(10000,0);
		private var menuRight:LoginMenu;
		private var menuLeft:LoginMenu;
		private var leftBackButton: LeftBackButton;
		private var rightBackButton: RightBackButton;
		private var dlgDisconnect:DialogBluetoothDisconnect;
		private  var studentArr:Array;
		
		public function SceneClass(preScene:MovieClip) {
			this.preScene = preScene; 
			this.sendCmd(BluetoothConnectCmd.make(User.deviceAddress));
		}
		protected override function onInit():void {
			trace("SceneClass onInit");
			this.dispatchEvent(new BgSoundEvent(BgSoundEvent.STOP));
			this.bg = new ClassBg();
			this.addChild(bg);
			//this.initMask();
			this.initMenuButton();
			this.initDisconnectDialog();
			//播放背景音乐
			bgSound = new ClassBgSound();
			//关闭背景音乐
			//bgSoundChannel = bgSound.play();
			//bgSoundChannel.addEventListener(Event.SOUND_COMPLETE,onBgSoundComplete);
			this.sendCmd(BluetoothOnlineCmd.make());
			_timer.addEventListener(TimerEvent.TIMER, _onTimer);
			_studentTimer.addEventListener(TimerEvent.TIMER, _onStudentTimer);
			this.initMenuButton();
			this.sendCmd(GetFlowerRankCmd.make());
			//播放背景音乐
			 _timer.start();
			 _studentTimer.start();
			 this.sendCmd(WindowMinCmd.make());
		}
		 
		 /*private function initMask():void {
			 var g:Graphics = this.graphics;
			 g.beginFill(0x000000);
   			 g.drawRect(0,0,this.stage.stageWidth,this.stage.stageHeight);
   			 g.endFill();
		 }*/
		 
		 
		private function _onTimer(e:TimerEvent):void
		{
			if(flowerData.length>0){
				this.findStudent(flowerData.shift());
			}
		}
		private function _onStudentTimer(e:TimerEvent):void
		{
			 this.sendCmd(GetFlowerRankCmd.make());
		}
		
		private function onBgSoundComplete(e:Event):void {
			bgSoundChannel = bgSound.play();
			bgSoundChannel.addEventListener(Event.SOUND_COMPLETE,onBgSoundComplete);
		}
		
		protected override function onRemove():void {
			
			this.dlgDisconnect.btnBack.removeEventListener(MouseEvent.CLICK,backClick);
			if(this.bgSoundChannel != null){
				this.bgSoundChannel.removeEventListener(Event.SOUND_COMPLETE,onBgSoundComplete);
			}
			
			this.leftBackButton.removeEventListener(MouseEvent.CLICK,backClick);
			this.rightBackButton.removeEventListener(MouseEvent.CLICK,backClick);
			
			this.sendCmd(new BluetoothOfflineCmd());
			//断开魔法棒连接
			//this.sendCmd(new BluetoothDisConnectCmd());
			//关闭背景音乐
			//启动主场景背景音乐
			this.dispatchEvent(new BgSoundEvent(BgSoundEvent.START));
			
			if(bgSoundChannel != null){
				bgSoundChannel.stop();
			}
			
			if(_timer){
				trace("计时器已关闭");
				_timer.stop();
			}
			if(_studentTimer){
				trace("计时器已关闭");
				_studentTimer.stop();
			}
		}
		//给红花动画添加头像
		private function onLoadComplete(e:Event):void {
			var loaderInfo = e.target as LoaderInfo;
			if(loaderInfo != this.loader.contentLoaderInfo){
				return;
			}
			var bitmap:Bitmap = loader.content as Bitmap;
			bitmap.smoothing = true;
			flowerAnim.setAvatar(bitmap);
		}
		//红花动画完成回调
		private function onAnimationComplete(e:FlowerAnimationEvent):void {
			this.removeChild(e.target as DisplayObject);
		}
		//初始化红花动画的数据
		protected  function findStudent(id:int):void {
			
			for(var i:int =0;i<studentArr.length;i++)
			{
				if(studentArr[i].babyId==id)
				{
					loader = new Loader();
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
					var req:URLRequest = new URLRequest(studentArr[i].avatar);
					loader.load(req);
					flowerAnim = new FlowerAnimation2();
					flowerAnim.setBabyName(studentArr[i].name);
					flowerAnim.addEventListener(FlowerAnimationEvent.COMPLETE,onAnimationComplete);
					this.addChild(flowerAnim);    
				}
			}
		}
		protected override function onCommandResponse(e:CmdEvent):void {
			super.onCommandResponse(e);
			var data:Object = e.data;
			if(data.cmd == BluetoothConnectCmd.COMMAND_ID){
				if(data.error == ""){ 
					trace("魔方课堂再次连接魔法棒成功");
				}else{
					trace("魔方课堂再次连接魔法棒失败");
				}
			}else if(data.cmd == BluetoothOnlineCmd.COMMAND_ID){
					if(data.error == ""){
					  if(data.data.baby_id!=null){
						   trace(data.data.baby_id+"在线红花状态")
						   flowerData.push(data.data.baby_id);
					   }
					}else if(data.error == BaseMc.ConnectFailed){
					   trace("弹出丢失连接窗口"); 
					   this.addChild(this.dlgDisconnect);
			        }else if(data.error == BaseMc.ConnectSuccess){
					   trace("窗口关闭"); 
					   this.removeChild(this.dlgDisconnect);
			        } else {
					    
			        }
			}else if(data.cmd == GetFlowerRankCmd.COMMAND_ID){
				if(data.error != null){
					studentArr = com.adobe.serialization.json.JSON.decode( data.error.toString());
				}else{
					//失败
					trace("班级成员请求数据失败");
				}
			}
		}
		private function initMenuButton():void {
			//var screenHeight = UtilScreen.getScreenHeight();
			//var screenWidth = UtilScreen.getScreenWidth();
			var screenHeight = this.stage.stageHeight;
			var screenWidth = this.stage.stageWidth;
			trace("screenHeight:" + screenHeight);
			trace("screenWidth:"+screenWidth);
			
			//右边位置
			this.menuRight = new LoginMenu();
			this.menuRight.scaleX = this.menuRight.scaleX * 0.7;
			this.menuRight.scaleY = this.menuRight.scaleY * 0.7;
			this.menuRight.y = screenHeight - 120;
			this.menuRight.x = screenWidth - 120;
			this.addChild(this.menuRight);
			
			//左边位置
			this.menuLeft = new LoginMenu();
			this.menuLeft.scaleX = this.menuLeft.scaleX * 0.7;
			this.menuLeft.scaleY = this.menuLeft.scaleY * 0.7;
			this.menuLeft.y = screenHeight - 120;
			this.menuLeft.x = 50;
			this.addChild(this.menuLeft);
			
			leftBackButton = new LeftBackButton();
			this.leftBackButton.scaleX = this.leftBackButton.scaleX * 0.7;
			this.leftBackButton.scaleY = this.leftBackButton.scaleY * 0.7;
			leftBackButton.x=150;
			leftBackButton.y=screenHeight-120;
			this.addChild(leftBackButton);
			
			rightBackButton = new RightBackButton();
			this.rightBackButton.scaleX = this.rightBackButton.scaleX * 0.7;
			this.rightBackButton.scaleY = this.rightBackButton.scaleY * 0.7;
			rightBackButton.x= screenWidth-220;
			rightBackButton.y=screenHeight-120;
			this.addChild(rightBackButton);
			
			this.leftBackButton.addEventListener(MouseEvent.CLICK,backClick);
			this.rightBackButton.addEventListener(MouseEvent.CLICK,backClick);
		}
		
		private function initDisconnectDialog():void {
			this.dlgDisconnect = new DialogBluetoothDisconnect();
			this.dlgDisconnect.btnBack.addEventListener(MouseEvent.CLICK,backClick);
		}
		
		private function backClick(e:MouseEvent):void {
			
			var dlg:DialogMessage = new DialogMessage("确定退出魔法课堂吗",false);
			dlg.addEventListener(MessageDialogEvent.OK,onUserBack);
			this.addChild(dlg);
		}
		
		private function onUserBack(e:MessageDialogEvent):void {
			this.remove();
			if(this.preScene)
			{
				this.preScene.show();
			}
		}
		
	}
	
}
